package com.tool.cn.algorithm.BPlusTree;

import com.tool.cn.algorithm.BPlusTree.one.CatalogBTree;
import com.tool.cn.algorithm.BPlusTree.two.BPlusTree;
import com.tool.cn.algorithm.BPlusTree.two.Product;

/**
 * 
 * @author wangzonghui
 * @date 2021年12月30日 上午10:34:32 
 * @Description B+ 树实现测试
 */
public class BPlusTreeTest {
	
	public static void main(String[] args) {
	      
//        oneTest();
        twoTest();

	}	
	
	/**
	 * 第一种实现测试
	 */
	public static void oneTest() {
		/**
         * 添加的顺序不同，最后树的结构也不同
         */
        CatalogBTree<Integer,String> bt = new CatalogBTree<>();
        bt.put(5,"a");
        bt.put(8,"b");
        //     bt.put(9,"c");
        bt.put(10,"d");
        bt.put(15,"e");
        //     bt.put(18,"f");
        bt.put(20,"g");
        bt.put(26,"h");
        //      bt.put(27,"i");
 
        bt.put(28,"i");
        bt.put(30,"j");
        //      bt.put(33,"k");
        bt.put(35,"l");
        bt.put(38,"m");
        //    bt.put(50,"n");
        bt.put(56,"o");
        bt.put(60,"p");
        //      bt.put(63,"q");
 
        bt.put(65,"r");
        bt.put(73,"s");
        //       bt.put(79,"t");
        bt.put(80,"u");
        bt.put(85,"v");
        //   bt.put(88,"w");
        bt.put(90,"x");
        bt.put(96,"y");
        bt.put(99,"z");
 
 
        /**
         * 插入第三个元素
         */
        bt.put(9,"c");
        bt.put(18,"f");
        bt.put(27,"i");
        bt.put(33,"k");
        bt.put(50,"n");
        bt.put(63,"q");
        bt.put(79,"t");
        bt.put(88,"w");
 
        System.out.println("高度："+bt.height());
        System.out.println("查询："+bt.get(9));
        System.out.println( bt.toString());
	}
	
	/**
	 * 第二种实现测试
	 */
	public static void twoTest() {
		BPlusTree<Product, Integer> b = new BPlusTree<>(4);

        long time1 = System.nanoTime();

        for (int i = 0; i < 10000; i++) {
            Product p = new Product(i, "test", 1.0 * i);
            b.insert(p, p.getId());
        }

      long time2 = System.nanoTime();

      Product p1 = b.find(345);
      System.out.println(p1.getName());

      long time3 = System.nanoTime();

      System.out.println("插入耗时: " + (time2 - time1));
      System.out.println("查询耗时: " + (time3 - time2));
	}

}
